package com.demo07.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyInterceptor implements HandlerInterceptor {

//    return true;放行，可以进入下一个拦截器


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//       放行：判断什么情况下登陆

        HttpSession session = request.getSession();
//        在login界面也放行
        if (request.getRequestURI().contains("Login")) {
            return true;
        }

//        含有session放行
        if (session.getAttribute("userLoginInfo") != null) {
            return true;
        }


//        假设用户没有登陆，需要重定向到其他界面

        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        return true;
    }
}
